using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;

public class Tester : MonoBehaviour
{
    public DrawUV drawuv;

    public Mesh mesh;
    public Vector2[] uv;

    private void OnGUI()
    {
        if (GUILayout.Button("Test"))
        {
            uv = mesh.uv;
            var size = drawuv.rectTransform.rect;


            Vector2[] arr1 = new Vector2[uv.Length];
            int[] triangles = mesh.triangles;

            for (int i = 0; i < triangles.Length; i += 3)
            {
                int index1 = triangles[i];
                int index2 = triangles[i + 1];
                int index3 = triangles[i + 2];

                Vector2 uv1 = uv[index1];
                Vector2 uv2 = uv[index2];
                Vector2 uv3 = uv[index3];
            }


            for (int i = 0; i < arr1.Length; i++)
            {
                arr1[i] = new Vector2(arr1[i].x * size.width, arr1[i].y * size.height);
            }

            // drawuv.uv = uv;
            // drawuv.ResetPoints(arr1,triangles);
        }

        GUI();
    }

    void GUI()
    {
        uv = mesh.uv;

        triangles = mesh.triangles;

        for (int i = 0; i < triangles.Length; i += 3)
        {
            int index1 = triangles[i];
            int index2 = triangles[i + 1];
            int index3 = triangles[i + 2];

            Vector2 uv1 = uv[index1] * rect + offset;
            Vector2 uv2 = uv[index2] * rect + offset;
            Vector2 uv3 = uv[index3] * rect + offset;
            // Handles.DrawSphere(uv1,size);
            // Handles.DrawSphere(uv2,size);
            // Handles.DrawSphere(uv3,size);
            // Handles.DrawLine(uv1, uv2);
            // Handles.DrawLine(uv2, uv3);
            // Handles.DrawLine(uv1, uv3);
        }
    }

    public float size = 0.1f;
    public int rect = 100;
    public Vector2Int offset = new Vector2Int(100, 100);
    public int[] triangles;

    void OnDrawGizmos()
    {
        uv = mesh.uv;

        triangles = mesh.triangles;

        for (int i = 0; i < triangles.Length; i += 3)
        {
            int index1 = triangles[i];
            int index2 = triangles[i + 1];
            int index3 = triangles[i + 2];

            Vector2 uv1 = uv[index1] * rect;
            Vector2 uv2 = uv[index2] * rect;
            Vector2 uv3 = uv[index3] * rect;
            Gizmos.DrawSphere(uv1, size);
            Gizmos.DrawSphere(uv2, size);
            Gizmos.DrawSphere(uv3, size);

            Gizmos.DrawLine(uv1, uv2);
            Gizmos.DrawLine(uv2, uv3);
            Gizmos.DrawLine(uv1, uv3);
        }
    }
}